package com.sky.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.pay.api.request.WeixinPayOrderRequest;
import com.sky.pay.common.OrderStatus;
import com.sky.pay.dao.OrderDao;
import com.sky.pay.service.OrderService;
import com.sky.pay.vo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.List;

/**
 * @author yangsk
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao, Order> implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Override
    public Order getExistOrder(WeixinPayOrderRequest request) {
        QueryWrapper<Order> orderQuery = new QueryWrapper<>();
        orderQuery.eq("order_no",request.getOrderNo());
        orderQuery.eq("user_id","从token中获取");
        orderQuery.eq("order_status", OrderStatus.NOPAY);
        orderQuery.eq("product_id",request.getProductId());
        orderQuery.eq("totle_fee",request.getTotleFee());
        return orderDao.selectOne(orderQuery);
    }

    @Override
    public Order createOrder(WeixinPayOrderRequest request) {
        Order order = new Order();
        order.setOrderNo("可根据自定义算法生成订单表好");
        order.setProductId(request.getProductId());
        order.setTotleFee(request.getTotleFee());
        //从token或者session中获取"
        order.setUserId(null);
        order.setOrderStatus(OrderStatus.NOPAY.toString());
        Date date = new Date();
        order.setCreateTime(date);
        order.setUpdateTime(date);
        orderDao.insert(order);
        return order;
    }

    @Override
    public void saveOrderPayCodeUrl(String orderNo, String codeUrl) {
        QueryWrapper<Order> orderUpdateWrapper = new QueryWrapper<>();
        //更新条件
        orderUpdateWrapper.eq("order_no",orderNo);
        //需要更新的字段
        Order order = new Order();
        order.setCodeUrl(codeUrl);
        orderDao.update(order,orderUpdateWrapper);
    }

    @Override
    public void updateOrderStatus(String orderNo,String orderStatus) {
        QueryWrapper<Order> orderUpdateWrapper = new QueryWrapper<>();
        //更新条件
        orderUpdateWrapper.eq("order_no",orderNo);
        //需要更新的字段
        Order order = new Order();
        order.setOrderStatus(orderStatus);
        orderDao.update(order,orderUpdateWrapper);
    }

    @Override
    public Order getOrderStatus(String orderNo) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_no",orderNo);
       return orderDao.selectOne(orderQueryWrapper);
    }

    /**
     * 查询超过5分钟，未支付的订单
     * @param minutes
     * @return
     */

    @Override
    public List<Order> getNoPayOrderByDuration(int minutes) {
        //mintus 为当前时间减去5s的差值
        Instant minus = Instant.now().minus(Duration.ofMillis(minutes));
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_status",OrderStatus.NOPAY.getType());
        //创建订单的时间要早于5分钟之前
        orderQueryWrapper.le("create_time",minus);
        return orderDao.selectList(orderQueryWrapper);
    }
}
